package com.zy.yun.controller.sys;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.yun.base.annotation.NotNullPara;
import com.zy.yun.base.common.DataTable;
import com.zy.yun.base.common.RestResult;
import com.zy.yun.base.constant.StatusEnum;
import com.zy.yun.base.entity.Role;
import com.zy.yun.base.entity.dto.RoleDTO;
import com.zy.yun.base.entity.vo.RoleVO;
import com.zy.yun.controller.common.BaseController;
import com.zy.yun.provider.service.IRoleResService;
import com.zy.yun.provider.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色 表 前端控制器
 * </p>
 *
 * @author 王彦委
 * @since 2019-10-20
 */
@Api(tags = "角色管理", description = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController<Role, RoleVO> {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IRoleResService roleResService;

    /**
     * @Author: 王彦委
     * @Description: 角色列表
     * @Date: 20:00 2019/10/20
     */

    @ApiOperation("角色列表")
    @PostMapping("findPage")
    public DataTable findPage(RoleDTO role, @RequestParam(defaultValue = "0") Integer pageNum, @RequestParam(defaultValue = "30") Integer pageSize) {


        EntityWrapper<Role> roleQueryWrapper = new EntityWrapper<>();

        if (StringUtils.isNotBlank(role.getName())) {
            roleQueryWrapper.like("name", role.getName());
        }
        if (StringUtils.isNotBlank(role.getStatus())) {
            roleQueryWrapper.eq("status", role.getStatus());
        }
        roleQueryWrapper.orderBy("createTime");

        return findPage(roleQueryWrapper, pageNum, pageSize);
    }

    /**
     * @Author: 王彦委
     * @Description: 增加角色
     * @Date: 20:27 2019/10/20
     */
    @ApiOperation("增加角色")
    @PostMapping("save")
    @NotNullPara({"name"})
    public RestResult save(RoleDTO role) {

        role.setCreateTime(new Date());
        role.setStatus(StatusEnum.STATUS_START.getCode());
        if (StringUtils.isNotBlank(role.getNote()) && role.getNote().length() > 50) {
            return RestResult.buildError("备注最多50个字！");
        }

        if (roleService.insert(role)) {
            return RestResult.buildSuccess();
        }
        return RestResult.buildError();
    }

    /**
     * @Author: 王彦委
     * @Description: 为角色分配权限
     * @Date: 21:48 2019/10/20
     */
    @PostMapping("saveAuth")
    @ApiOperation("为角色分配资源权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id"),
            @ApiImplicitParam(name = "resIds", value = "资源id,用逗号隔开")
    })
    @NotNullPara({"roleId"})
    public RestResult saveAuth(Long roleId, @RequestParam(value = "resIds") List<Long> resIds) {

        if (roleResService.saveAuth(roleId, resIds)) {
            return RestResult.buildSuccess();
        }
        return RestResult.buildError();
    }


    /**
     * @Author: 王彦委
     * @Description: 分配给角色数据权限，并保存
     * @Date: 10:45 2019/11/10
     */
    @ApiOperation("为角色分配数据权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id"),
            @ApiImplicitParam(name = "organizationIds", value = "组织id,用逗号隔开")
    })
    @PostMapping("saveDataAuth")
    public  RestResult saveDataAuth(Long roleId, @RequestParam(value = "organizationIds") List<Long> organizationIds) {


        if (roleResService.saveDataAuth(roleId, organizationIds)) {
            return RestResult.buildSuccess();
        }
        return RestResult.buildError();
    }

    /**
     * @Author: 王彦委
     * @Description: 根据状态条件角色
     * @Date: 20:50 2019/10/21
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "1:开启 2：暂停"),
    })
    @ApiOperation("根据状态条件角色")
    @PostMapping("findRoleByStatus")
    public RestResult findRoleByStatus(RoleDTO role) {

        EntityWrapper<Role> roleQueryWrapper = new EntityWrapper<>();
        roleQueryWrapper.eq("status", role.getStatus());
        List<Role> list = roleService.selectList(roleQueryWrapper);

        return RestResult.buildSuccess(list);
    }
}
